/*
 * @Author: wangming
 * @Date: 2021-06-02 19:40:30
 * @LastEditors: wangming
 * @LastEditTime: 2022-10-20 16:10:22
 * @FilePath: /sinocare_detection_sdk_wechrt/pages/connectDetail/jw/index.js
 * @Description: 
 */
// pages/connectDetail/deviceDetail.js
const app = getApp();
const JinWenAdapter = require('mcbluetoothsdk/lib/JinWenAdapter');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    textLog: "",
    deviceId: "",
    name: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let devid = decodeURIComponent(options.deviceId); // deviceUUId
    let macAddr = decodeURIComponent(options.macAddr); // macId
    let devname = decodeURIComponent(options.name); // 设备名称

    let params = app.globalData.authParams // 鉴权信息
    params.deviceMac = macAddr

    JinWenAdapter.createBLEConnection( devid, function(res) {
      let {code, msg, data} = res
      if (code == 0) {
        that._onBLECharacteristicValueChange();
        let log = that.data.textLog + '连接成功' + "\n";
        that.setData({
          textLog: log
        })
      } else {
        wx.showToast({
          title: msg,
          icon: 'error',
          duration: 2000
        })
        let log = that.data.textLog + JSON.stringify(res) + "\n";
        that.setData({
          textLog: log
        })
        console.error(data)
      }
    }, wx, 20000, params)


    that.setData({
      deviceId: devid,
      name: devname
    });
  },
  onShow: function () {
    if (wx.setKeepScreenOn) {
      wx.setKeepScreenOn({
        keepScreenOn: true,
        success: function (res) {
          console.log('正保持屏幕常亮')
        }
      })
    }
  },
  _onBLECharacteristicValueChange() {
    let that = this
    wx.onBLECharacteristicValueChange((res) => {
      let obj = JinWenAdapter.parseData(res)
      if (obj && typeof obj === 'object') {
        let log = that.data.textLog + JSON.stringify(obj) + "\n";
        that.setData({
          textLog: log
        })
      }
    });
  },
  /**
   * @description: 获取历史记录
   * @param {*}
   * @return {*}
   */
  _getHistory() {
    JinWenAdapter.sendGetHistCode( this.data.deviceId, function(res) {
      console.log(res)
    }, wx)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  clearLogs: function () {
    this.setData({
      textLog: ""
    });
  },
  closeConnection: function () {
    let that = this
    wx.closeBLEConnection({
      deviceId: that.data.deviceId,
      success (res) {
        console.log(res)
        wx.reLaunch({
          url: '/pages/selectDevice/startpage'
        });
      },
      fail (err) {
        console.log(err)
        if (err && typeof obj === 'err') {
          let log = that.data.textLog + JSON.stringify(err) + "\n";
          that.setData({
            textLog: log
          })
        }
      }
    })
  },
  unbindDevice: function() {
    wx.reLaunch({
      url: '/pages/selectDevice/startpage'
    });
  }
})